package com.example.shw

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.DisplayMetrics
import android.view.MotionEvent
import android.view.View
import android.view.Window
import android.view.WindowManager
import com.example.shw.wedgit.PlaneView

class GameActivity : AppCompatActivity(){
    private val speed = 10
    private var planeView : PlaneView ?= null
    var metrics : DisplayMetrics ?= null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //去掉窗口标题
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        //全屏显示
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)

        planeView = PlaneView(this)
        setContentView(planeView)
        planeView?.setBackgroundResource(R.drawable.background)

        val windowManager = windowManager
        val display = windowManager.defaultDisplay
        metrics = DisplayMetrics()

        display.getMetrics(metrics)

        planeView?.currentX = (metrics!!.widthPixels / 2).toFloat()
        planeView?.currentY = (metrics!!.heightPixels / 2).toFloat()
        planeView?.setOnTouchListener(MyTouchListener())
    }
    inner class MyTouchListener : View.OnTouchListener{
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            if (event!!.x < metrics!!.widthPixels / 8)
                planeView!!.currentX -= speed
            if (event.x > metrics!!.widthPixels * 7 / 8)
                planeView!!.currentX += speed
            if (event.y < metrics!!.heightPixels / 8)
                planeView!!.currentY -= speed
            if (event.y > metrics!!.heightPixels * 7 / 8)
                planeView!!.currentY += speed
            return true
        }
    }
}